
<div class="hidden" id="addOT-cont">
    <form class="well form-inline margin-horisontal login" action="" method="POST" name="addOTForm" id="addOTForm">
        <fieldset>

            <input type="hidden" name="action" id="action" value="<?php echo ACTIONS::INSTALL_ADD_OBJTYPE; ?>">

            <div class="control-group">
                <label class="control-label" for="ot_name">Название</label>
                <div class="controls">
                    <input type="text" class="input-xlarge" id="ot_name" name="ot_name" value="<?php echo Arr::getValue($params, 'ot_name', ''); ?>">
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="ot_datatype">Тип данных</label>
                <div class="controls">
                    <select id="ot_datatype" name="ot_datatype" class="input-xlarge">

                        <?php
                        $selvalue = Arr::getValue($params, 'datatype', 0);
                        $datatypes = Arr::getValue($params, 'datatypes', array());
                        foreach ($datatypes as $key=>$val) {
                            $sel = ($key == $selvalue)? 'selected="selected"' : '';
                            $t = $key? ', '.Arr::getValue($val, 'unit', '') : '';
                            echo '<option value="'.$key.'" '.$sel.'>'.Arr::getValue($val, 'name', '-').$t.'</option>';
                        }
                        ?>
                    </select>
                    <span class="help-inline"></span>
                </div>
            </div>



            <div class="form-actions">
                <button type="submit" class="btn btn-primary" id="addOT-submit"><i class="icon-ok icon-white"></i> Сохранить</button>
                <button type="reset" class="btn btn-danger" id="addOT-cancel"><i class="icon-remove icon-white"></i> Отменить</button>
            </div>
        </fieldset>
    </form>
</div>


<script>

    function addOTDialogOpen(callback) {
        callback = callback || function() {};
        if (Elm.hasOwnProperty('addOT') && Elm.addOT) Elm.addOT.dialog('open');
        Elm.addOTForm.successCallback = callback;
    }

    $(function() {
        Elm.addOT = $('#addOT-cont');
        Elm.addOTForm = $('#addOTForm');
        Elm.addOTForm.successCallback = function(data) {};


        Elm.addOT.dialog({
            width       : 350,
            modal       : true,
            resizable   : false,
            title       : "Добавить тип объекта",
            autoOpen    : false,
            buttons     : {}
        }).removeClass('hidden');



        Elm.addOTForm.submit(function(){
            var send = Elm.addOTForm.serialize();
            jsonRequest(
                'objtypes.php',
                send,

                function(data) {
                    if(checkSession(data)) {
                        if (!data.state) {
                            uiAlert(data.message,'Error');
                            return;
                        }

                        if (data.hasOwnProperty('response_data') && data.response_data) {
                            var resp = data.response_data;
                            if (resp.hasOwnProperty('insertedId')) {

                                var data = {
                                    datatype : $('#ot_datatype').find('option[value="'+$('#ot_datatype').val()+'"]').html(),
                                    id : resp.insertedId
                                }

                                if (
                                    Elm.addOTForm.hasOwnProperty('successCallback')
                                    && (typeof(Elm.addOTForm.successCallback) == 'function')
                                ) {
                                    var cb = Elm.addOTForm.successCallback;
                                    Elm.addOTForm.successCallback = function() {};
                                    cb(data);
                                }
                            }
                        }
                        Elm.addOT.dialog('close');
                    }
                    else Elm.loginFormModal.dialog('open');
                },

                function() {
                    uiAlert('Ошибка соединения','Error');
                }
            );
                return false;
        });


        $('#addOT-cancel').click(function(){
            Elm.addOT.dialog('close');
            return false;
        });
    });


</script>